﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dokan;
using CloudDrive.S3;
using log4net;
using Affirma.ThreeSharp.Wrapper;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace TestCloudDrive
{
    public partial class Form1 : Form
    {
        ILog log = log4net.LogManager.GetLogger(typeof(Form1));
        public Form1()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            DokanOptions options = new DokanOptions()
            {
                DebugMode = true,
                DriveLetter = 'Z',
                ThreadCount = 1,
                VolumeLabel = "S3",
                UseKeepAlive = false
            };
            
            S3DriveInformation info = new S3DriveInformation(txtAccessKey.Text, txtSecretKey.Text, txtBucketName.Text);
            FileProxy proxy = new FileProxy(info);
            S3DriveOperations operations = new S3DriveOperations(proxy);
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            {
                DokanNet.DokanMain(options, operations);
            }));
            button1.Enabled = false;
            //var dokan = DokanNet.DokanMain(options, operations);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DokanNet.DokanUnmount('Z');
            button1.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ThreeSharpWrapper wrapper = new ThreeSharpWrapper(txtAccessKey.Text, txtSecretKey.Text);
            string result = wrapper.ListBucket(txtBucketName.Text);
            string tempFile = Path.GetTempFileName();
            tempFile = tempFile + ".xml";
            File.WriteAllText(tempFile, result);
            Process.Start("iexplore.exe", tempFile);
        }
    }
}
